package algotithm.leetcode.matrix.test1380;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 25068
 * @Date 2021/5/8 10:34
 * @Title 1380. 矩阵中的幸运数
 */
public class Solution {

    public static void main(String[] args) {
        int[][] matrix = {{3,7,8},{9,11,13},{15,16,17}};
        Solution solution = new Solution();
        List<Integer> res = solution.luckyNumbers(matrix);
        for (int i = 0; i < res.size(); i++) {
            System.out.print(res.get(i));
        }
    }

    public List<Integer> luckyNumbers (int[][] matrix) {
        int r = matrix.length, c = matrix[0].length;
        int[] rMin = new int[r];
        Arrays.fill(rMin, Integer.MAX_VALUE);
        int[] cMax = new int[c];
        for (int i = 0; i < r; ++i) {
            for (int j = 0; j < c; ++j) {
                rMin[i] = Math.min(rMin[i], matrix[i][j]);
                cMax[j] = Math.max(cMax[j], matrix[i][j]);
            }
        }

        List<Integer> ans = new ArrayList<Integer>();
        for (int i = 0; i < r; ++i) {
            for (int j = 0; j < c; ++j) {
                if (matrix[i][j] == rMin[i] && matrix[i][j] == cMax[j]) {
                    ans.add(matrix[i][j]);
                }
            }
        }
        return ans;
    }


}
